package com.yyge.controller.admin.merchandiser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyge.common.controller.BaseController;
import com.yyge.common.enums.AckCode;
import com.yyge.common.util.WaimaiThreadLocal;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.common.vo.resp.R;
import com.yyge.controller.admin.login.resp.AdminLoginResp;
import com.yyge.controller.admin.mapstruct.MerchandiserConverter;
import com.yyge.controller.admin.merchandiser.req.MerchandiserReq;
import com.yyge.controller.admin.merchandiser.req.ProcessReq;
import com.yyge.controller.admin.merchandiser.resp.MerchandiserResp;
import com.yyge.entity.EnterpriseProcess;
import com.yyge.entity.Merchandiser;
import com.yyge.service.EnterpriseProcessService;
import com.yyge.service.MerchandiserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Objects;

@RestController
@RequestMapping("/api/merchandiser")
public class MerchandiserController extends BaseController<Merchandiser,Object> {
    public MerchandiserController() {
        this.converter=MerchandiserConverter.class;
    }
    // 注入业务逻辑层
    private MerchandiserService service;


    @Autowired
    private EnterpriseProcessService enterpriseProcessService;



    @Autowired/*自动装配*/
    public void setService(MerchandiserService merchandiserService){
        this.service=merchandiserService;
    }

    @GetMapping("data")/* GET(SELECT): 查询 */
    public R<PageResp<MerchandiserResp>> get(MerchandiserReq req) throws Exception  {
        QueryWrapper wrapper = this.getWrapper(req);

        //判断类型
        AdminLoginResp adminLoginResp= WaimaiThreadLocal.getCurrent();
        if(adminLoginResp.getType() == 2){}

        PageResp<MerchandiserResp> pageResp =this.page(req,pageInfo ->service.page(pageInfo,wrapper));
        return R.okHasData(pageResp);
    }


    @PostMapping/* POST(CREATE): 新增&登陆 */
    public R post(@RequestBody Merchandiser merchandiser){
        Boolean i=service.save(merchandiser);
        return i?R.ok("新增成功！"):R.error(AckCode.SYSTEM_SERVICE_ADD_ERROR);
    }

    @PutMapping/* POST(CREATE): 更新 */
    public R out(@RequestBody Merchandiser merchandiser){
        Boolean i=service.updateById(merchandiser);
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_MODIFY_ERROR);
    }

    @PatchMapping/* PUT(UPDATE): 部分更新 */
    public R patch(@RequestBody Merchandiser merchandiser){
        return null;
    }

    @DeleteMapping("/{id}")/* DELETE(DELETE): 删除&&批量删除 */
    public R delete(@PathVariable("id") Integer[] list){
        boolean i = service.removeByIds(Arrays.asList(list));
        return i?R.ok("修改成功！"):R.error(AckCode.SYSTEM_SERVICE_DELETE_ERROR);
    }

    /**
     * 审核2个表操作
     * 1）、更新原来商家信息表       update
     * 2)、EnterpriseProcess新增一笔数据   insert
     * @param req
     * @return
     */
    @ApiOperation(value = "审核")
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @PutMapping("/process")
    public R process(@RequestBody @Valid ProcessReq req){
        Merchandiser merchandiser = MerchandiserConverter.MAPPER.convert(req);
        System.out.println("看看是不是0");
        System.out.println(merchandiser);

        Merchandiser db = this.service.getById(req.getTenantId());
        if(Objects.isNull(db)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        //如果商家状态不是0，就不能进行审核
        if(!db.getStatus().equals("0")){
            return R.build(AckCode.ENTERPRISE_STATUS_ERROR);
        }
        db.setStatus(merchandiser.getStatus());
        boolean success = service.updateById(db);
        if(success){
            //插入审核数据
            EnterpriseProcess process=new EnterpriseProcess();
            process.setTenantId(db.getId());
            process.setDescription(req.getDescription());
            //审核数据中的状态 0审核通过 2审核失败
            process.setStatus(db.getStatus());
            success=this.enterpriseProcessService.save(process);
        }

        if(success){
            //发送短信 (简单工厂方法模式)
//            SmsRequestReq sms=null;
//            if(req.getStatus().equals("1")){
//                sms = new Sms1455683(db.getLoginName(), db.getName());
//            }else {
//                sms = new Sms1770265(db.getLoginName(), req.getDescription());
//            }
//            SmsTemplate smsTemplate=new SmsTemplate();
//            smsTemplate.sendSms(sms);
        }

        return success?R.ok():R.build(AckCode.DATABASE_ERROR);
    }
}

